<template>
  <layout>
    <template slot="title">
      <b>死亡人员列表</b>
    </template>
    <!-- 顶部操作 -->
    <section slot="search">
      <custom-form
        ref="search-form"
        class="search-form"
        v-model="tableSearch"
        layout="inline"
        mode="search"
        :FormList="searchColumns"
      >
        <a-button
          class="action-button"
          type="primary"
          icon="search"
          @click="search()"
          >搜索</a-button
        >
        <a-button class="action-button" icon="plus" type="primary" @click="handlerTableAdd"
          >新增人员</a-button
        >
        <a-button
          type="primary"
          icon="upload"
          class="action-button"
          @click="
            tableImportExecl({
              headers: 1,
              title: '农户减员列表',
            }, {
              s_a_id: currentAreaId
            })
          "
          >导入</a-button
        >
        <a-button
          type="primary"
          icon="download"
          class="action-button"
          @click="handlerTableExportExecl(true)"
          >导出</a-button
        >

        <a-button
          type="default"
          icon="download"
          @click="handlerDownload($downloadDeadMembers)"
        >
          下载模板
        </a-button>
      </custom-form>
    </section>

    <!-- 产品列表 -->
    <a-table
      :dataSource="tableData"
      :columns="tableColumns"
      :rowKey="(record) => record.iid"
      :pagination="false"
      :loading="tableLoading"
      @change="handleTableChange"
      :customRow="customTableRow"
      :scroll="{
        y: tableClientHeight + 70,
        x: '100%',
      }"
      bordered
    >
      <!-- 按钮区 -->
      <template slot="action" slot-scope="row, record, index">
        <a-button
          class="action-button"
          type="link"
          size="small"
          @click.stop="handleTableEdit(row)"
          >编辑</a-button
        >
        <a-popconfirm
          title="您确认要删除该条数据?"
          ok-text="确认"
          cancel-text="取消"
          @confirm="tableDel(row)"
        >
          <a-button class="action-button" type="link" size="small" @click.stop
            >删除</a-button
          >
        </a-popconfirm>
      </template>
    </a-table>

    <!-- 产品列表分页器 -->
    <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      showSizeChanger
      :show-total="showTotal"
      :pageSize="pageSize"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination>

    <!-- 编辑产品数据 -->
    <a-modal
      :visible="visible"
      :title="visibleTitle"
      :width="500"
      centered
      @cancel="visible = false"
    >
      <custom-form
        ref="custom-form"
        class="custom-form"
        v-model="currentRow"
        :FormList="formColumns"
        :mode="formMode"
        v-bind="formItemLayout"
        layout="horizontal"
      ></custom-form>
      <section slot="footer">
        <a-button type="primary" @click="handleSubmit">提交</a-button>
      </section>
    </a-modal>
  </layout>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue, Prop } from 'vue-property-decorator'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin, TableExtendImport } from '@/mixin'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import { PayUnitType } from '@/common/enum'
import { famerServices } from '@/common/services/famer'
import { userModule } from '@/store/modules/user'
import { cropMemberServices } from '@/common/services/crop-member'
import subsidyServices from '@/common/services/subsidy'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm,
  },
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableExtendImport,
  AuthMixin
) {
  @Prop({
    type: String,
  })
  public currentAreaId!: string

  public commonParams = {
    s_a_id: '',
  }

  get isLarge() {
    return userModule.isLarge
  }

  public hasTotalRow = false

  public isLocalSort = true

  // 产品列表搜索条件
  public tableSearch = {}

  // 产品列表表格信息
  public columns: ITable.IColumn[] = [
    {
      title: '行政编码',
      dataIndex: 's_a_id',
      key: 's_a_id',
      width: 130,
    },
    {
      title: '行政区',
      dataIndex: 's_a_nam',
      key: 's_a_nam',
      width: 300,
    },
    {
      title: '姓名',
      dataIndex: 'member_nam',
      key: 'member_nam',
      width: 120,
      form: {
        type: 'a-input',
        placeholder: '请输入农户姓名',
        isShowQuery: true,
      },
    },
    {
      title: '身份证号',
      dataIndex: 'member_sfz',
      key: 'member_sfz',
      width: 160,
      form: {
        type: 'a-input',
        placeholder: '请输入农户身份证号',
        isShowQuery: true,
      },
    },
    {
      title: '原地址',
      dataIndex: 'member_addr',
      key: 'member_addr',
      width: 160,
      form: {
        type: 'a-textarea',
        placeholder: '请输入原地址',
        isShowQuery: true,
      },
    },
    {
      title: '死亡日期',
      dataIndex: 'dead_time',
      key: 'dead_time',
      width: 120,
      form: {
        type: 'a-date-picker',
        valueFormat: 'YYYY-MM-DD',
        placeholder: '请选择死亡日期',
      },
    },
    {
      title: '备注',
      dataIndex: 'readme',
      key: 'readme',
      width: 200,
      form: {
        type: 'a-textarea',
        placeholder: '请输入备注',
      },
    },
    {
      title: '建档人',
      dataIndex: 'i_usr_nickname',
      key: 'i_usr_nickname',
      width: 120,
      form: {
        isExport: false,
        isShowFrom: false,
      },
    },
    {
      title: '建档时间',
      dataIndex: 'i_time',
      key: 'i_time',
      width: 150,
      form: {
        isExport: false,
        isShowFrom: false,
      },
    },
    {
      title: '更新人',
      dataIndex: 'u_usr_nickname',
      key: 'u_usr_nickname',
      width: 120,
      form: {
        isExport: false,
        isShowFrom: false,
      },
    },
    {
      title: '更新时间',
      dataIndex: 'u_time',
      key: 'u_time',
      width: 150,
      form: {
        isExport: false,
        isShowFrom: false,
      },
    },
    {
      title: '操作',
      key: 'action',
      width: 100,
      fixed: 'right',
      scopedSlots: { customRender: 'action' },
    },
  ]

  public tableApi = {
    list: famerServices.getDeadMemberList,
    add: famerServices.updateDeadMember,
    edit: famerServices.updateDeadMember,
    del: famerServices.delDeadMember,
    import: famerServices.ImportDeadMembers,
    exportExecl: famerServices.ExportDeadMembers
    // exportExecl: cropMemberServices.exportCropMemberCount,
  }

  @Watch('currentAreaId', { immediate: true })
  public handlerCurrentAreaIdChange() {
    this.commonParams.s_a_id = this.currentAreaId
    this.getData()
  }

  public mounted() {}

  public async getData() {
    const res = await this.getTableList()
    this.tableData = res.data.data || []
  }

  public handlerTableAdd() {
    this.tableAdd()
    this.$nextTick(() => {
      this.currentRow = Object.assign(
        {},
        { s_a_id: this.currentAreaId, role: 1 }
      )
    })
  }

  public handlerTableExportExecl(isSupportSort = false) {
    let extra: Record<string, string> = {}
    this.tableExportExecl(extra, false, {
      title: isSupportSort ? '设置导出Execl电子表的字段及顺序' : '',
      isSupportSort
    })
  }

  // 下载
  public handlerDownload(link: string) {
    const a = document.createElement('a')
    a.href = link
    a.target = '_blank'
    a.click()
  }

  // 编辑前数据预处理
  public async handleTableEdit(row: any) {
    this.tableEdit(Object.assign({}, row))
  }
}
</script>

<style scoped lang="less">
.action-button {
  margin: 0px 10px 0px 10px;
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}
/deep/ .ant-table {
  th[key='member_total'],
  th[key='plant'],
  th[key^='pay'],
  th[key='pay_price_total'] {
    text-align: center !important;
  }

  thead tr {
    background: #fafafa;
  }

  tbody tr {
    background: #fff;
  }

  th[key='s_a_nam'],
  tr td:nth-of-type(3) {
    position: sticky;
    left: 0;
    z-index: 3;
    background: inherit;
  }
}
// /deep/ .ant-table-thead th {
//   text-align: center!important;
// }
</style>
